package org.unige.mpej.eckmann.sonedyan.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * handler for MySQL connections
 */
public class DatabaseHandler
{
private static Logger log = Logger.getLogger(org.unige.mpej.eckmann.sonedyan.db.DatabaseHandler.class);
	
	private static final String HOST = "localhost";
	private static final String PORT = "3306";
	private static final String DB_NAME = "twitter";
	private static final String USERNAME = "jimmy";
	private static final String PASSWORD = "Ahbh37-4ta";
	private static final String DRIVER = "com.mysql.jdbc.Driver";
	private static final String TYPE = "mysql";
	
	private Connection conn;
	
	/**
	 * default constructor
	 */
	public DatabaseHandler()
	{
		try
		{
			String url = "jdbc:" + TYPE + "://" + HOST + ":" + PORT + "/" + DB_NAME;
			Class.forName(DRIVER);
			
			Properties props = new Properties();
			props.setProperty("user", USERNAME);
			props.setProperty("password", PASSWORD);

			this.conn = DriverManager.getConnection(url, props);
		}
		catch(Exception e)
		{
			log.error("An error occured: " + e.getMessage());
		}
	}
	
	/**
	 * execute specified select query
	 */
	public ResultSet execute(String query)
	{
		try
		{
			Statement s = this.conn.createStatement();
			return s.executeQuery(query);
		}
		catch(Exception e)
		{
			log.error("An error occured while executing query '" + query + "': " + e.getMessage());
			return null;
		}
	}
	
	/**
	 * execute specified select prepared query
	 */
	public ResultSet execute(PreparedStatement s)
	{
		try
		{
			return s.executeQuery();
		}
		catch(Exception e)
		{
			log.error("An error occured while executing prepared query: " + e.getMessage());
			return null;
		}
	}
	
	/**
	 * insert and get primary key
	 */
	public int executeAndGetKey(PreparedStatement s)
	{
		try
		{
			ResultSet result = this.execute(s);
			result.next();
			return result.getInt(1);
		}
		catch(Exception e)
		{
			log.error("An error occured while executing prepared query: " + e.getMessage());
			return -1;
		}
	}
	
	/**
	 * execute specified update query
	 */
	public int update(String query)
	{
		try
		{
			Statement s = this.conn.createStatement();
			return s.executeUpdate(query);
		}
		catch(Exception e)
		{
			log.error("An error occured while executing query '" + query + "': " + e.getMessage());
			return -1;
		}
	}
	
	/**
	 * execute specified update prepared query
	 */
	public int update(PreparedStatement s)
	{
		try
		{
			return s.executeUpdate();
		}
		catch(Exception e)
		{
			log.error("An error occured while executing prepared query: " + e.getMessage());
			return -1;
		}
	}
	
	/**
	 * close DB connection
	 */
	public boolean close()
	{
		try
		{
			this.conn.close();
			return true;
		}
		catch(Exception e)
		{
			log.error("Unable to close DB connection: " + e.getMessage());
			return false;
		}
	}

	/**
	 * get the DB connection instance
	 */
	public Connection getConn()
	{
		return conn;
	}
	
}
